package com.shyroke.daydayzhuanweb.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.shyroke.daydayzhuanapi.constant.ProjectConst;
import com.shyroke.daydayzhuanapi.constant.ProjectFromConstant;
import com.shyroke.daydayzhuanapi.dto.*;
import com.shyroke.daydayzhuanapi.entity.*;
import com.shyroke.daydayzhuandao.respository.NewsRepository;
import com.shyroke.daydayzhuandao.respository.ProjectRepository;
import com.shyroke.daydayzhuandao.respository.ProjectSubjectRepository;
import com.shyroke.daydayzhuandao.respository.SubjectRepository;
import com.shyroke.daydayzhuansvc.service.ICommentService;
import com.shyroke.daydayzhuansvc.service.impl.*;
import com.shyroke.daydayzhuansvc.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 类名 ：专题的控制层
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2018/12/5 11:47
 */
@Controller
@RequestMapping(value = "subject")
public class SubjectController {

    @Autowired
    private ProjectSubjectRepository projectSubjectRepository;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private AppService appService;
    @Autowired
    private TypeService typeService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 跳转到专题详情页面
     * @param subjectId 专题id
     * @param model
     * @return
     */
    @GetMapping(value = "/{subjectId}")
    public ModelAndView desc3( @PathVariable("subjectId")Integer subjectId, ModelAndView model, HttpServletRequest request, HttpServletResponse response){
        SubjectHomeDto subject = subjectService.findById(subjectId);
        if(subject == null){
            model.setViewName("error/404");
            response.setStatus(HttpStatus.SC_NOT_FOUND);
            return model;
        }
        TypeDto type = typeService.findByUrl("/subject");

        model = UserUtils.getMenuList(model,type.getId());
//        subjectService.updateReadAmount(request,subjectId);
        if(subject != null){
            if(subject.getCreateTime()!=null){
                subject.setShowCreateTime(CommonUtil.getDateByFormat(subject.getCreateTime(),"yyyy-MM-dd"));
                model.addObject("pubdate", DateUtils.format(subject.getCreateTime(),"yyyy-MM-dd'T'HH:mm:ss"));
            }
            if(subject.getUpdateTime() != null){
                model.addObject("update",DateUtils.format(subject.getUpdateTime(),"yyyy-MM-dd'T'HH:mm:ss"));
                model.addObject("showUpdateTime",DateUtils.format(subject.getUpdateTime(),"yyyy-MM-dd"));
            }
        }

        //专题包含的文章 - 阅读量最高
        List<ProjectDto> projectList = projectService.findBySubjectId(subjectId);

        //专题包含的最新文章
        List<ProjectDto> newProjectList = projectService.findNewProjectBySubjectId(subjectId);

        //app排行
        List<AppDto> appList = appService.findByTop(10,false);
        model.addObject("appList", appList);
        model.addObject("projectList", projectList);
        model.addObject("newProjectList", newProjectList);
        model.addObject("type",type);
        model.addObject("subject",subject);
        model.setViewName("article/subject_desc");
        return model;
    }

    @PostMapping("updateCount")
    @ResponseBody
    public void updateCount(@RequestParam("subjectId") Integer subjectId){
        subjectService.updateCount(subjectId);
    }

}


